package com.ruoyi.web.controller.app;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.order.domain.UserAddress;
import com.ruoyi.order.domain.app.Vo.ProductViewDetail;
import com.ruoyi.order.domain.app.Vo.ProductViewResultVo;
import com.ruoyi.order.domain.app.Vo.UserAddressVo;
import com.ruoyi.order.domain.app.from.address.AddressFrom;
import com.ruoyi.order.mapper.UserAddressMapper;
import com.ruoyi.order.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/app")
public class UserAddressAppController {
    @Resource
    private UserAddressMapper userAddressMapper;


    @RequestMapping("/p/address/list")
    @ResponseBody
    public List<UserAddressVo> list(){
        Map map = new HashMap();
        // TODO: 2022/2/9 userId 登录情况下获取，测试数据 为 null
        map.put("userID" ,  null  ) ;
        List<UserAddressVo>  userAddressVos = userAddressMapper.appSelectList(map);
        return userAddressVos;
    }

    @RequestMapping("/p/address/addrInfo/{addressID}")
    @ResponseBody
    public UserAddressVo getAddress(@PathVariable(value = "addressID", required = false) Integer addressID){
        Map map = new HashMap();
        // TODO: 2022/2/9 userId 登录情况下获取，测试数据 为 null
        map.put("addressID" , addressID);
        List<UserAddressVo>  userAddressVos = userAddressMapper.appSelectList(map);
        UserAddressVo userAddressVo = new UserAddressVo() ;
        if (userAddressVos !=null && userAddressVos.size() > 0 ){
            userAddressVo = userAddressVos.get(0);
        }
        return userAddressVo;
    }
    @RequestMapping("/p/address/addAddr")
    public String addAddress(AddressFrom addressFrom){
        UserAddress userAddress = new UserAddress();
        trans(addressFrom,userAddress);
        userAddressMapper.insertUserAddress(userAddress);
        return "添加地址成功";
    }
    public void trans(AddressFrom addressFrom ,UserAddress userAddress){
        userAddress.setAcceptPerson(addressFrom.getReceiver());
        userAddress.setMobile(addressFrom.getMobile());
        userAddress.setDetail(addressFrom.getAddr());
        userAddress.setProvinceID(addressFrom.getProvinceId());
        userAddress.setCityName(addressFrom.getCity());
        userAddress.setCityID(addressFrom.getCityId());
        userAddress.setAreaName(addressFrom.getArea());
        userAddress.setAddressID(addressFrom.getAreaId());
    }

}
